package com.naire.modul.projeckpi.dao.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.common.dao.GeneralDaoImpl;
import net.common.service.PageBean;
import net.common.util.BeanUtils;

import com.naire.modul.kpi.pojo.Kpi;
import com.naire.modul.projeckpi.dao.ProjeckpiDao;
import com.naire.modul.projeckpi.pojo.Projeckpi;
import com.naire.modul.projeckpi.vo.ProjeckpiVo;
import com.naire.modul.project.pojo.Project;

public class ProjeckpiDaoImpl extends GeneralDaoImpl implements ProjeckpiDao {

	@SuppressWarnings("unchecked")
	@Override
	public List<ProjeckpiVo> search(PageBean pageBean, Integer project_id,
			String parent_code) throws Exception {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("project_id", project_id);
		StringBuilder hql = new StringBuilder(
				"from Projeckpi where project.project_id=:project_id");
		if (parent_code != null && !parent_code.equals("")) {
			hql.append(" and parent_num=:parent_num");
			params.put("parent_num", parent_code);
		} else {
			hql.append(" and parent_num is null");
		}
		List<ProjeckpiVo> d = new ArrayList<ProjeckpiVo>();
		List<Projeckpi> l = hibernateHandler.query(hql, pageBean, params);
		for (Projeckpi bean : l) {
			ProjeckpiVo dv = new ProjeckpiVo();
			BeanUtils.copyProperties(bean, dv);
			d.add(dv);
		}
		return d;
	}

	@Override
	public void insert(ProjeckpiVo bean) throws Exception {
		Project project = (Project) hibernateHandler.find(Project.class,
				bean.getProject_id());
		String[] kpiIds = bean.getKpi_ids().split(",");
		if (kpiIds != null) {
			int i = 0;
			for (String kpiId : kpiIds) {
				Kpi kpi = (Kpi) hibernateHandler.find(Kpi.class, Integer.parseInt(kpiId));
				Projeckpi pojo = findByProjectAndKpi(bean.getProject_id(), kpi.getKpi_num());
				if(pojo == null) {
					pojo = new Projeckpi();
					pojo.setProject(project);
					pojo.setKpi_name(kpi.getKpi_name());
					pojo.setKpi_num(kpi.getKpi_num());
					pojo.setSource(new BigDecimal(0));
					pojo.setDisable(0);
					pojo.setParent_num(kpi.getParent_num());
					hibernateHandler.insert(pojo);
					if(i % 20 == 0) {
						hibernateHandler.flush();
					}
					i++;
				}
			}
		}
	}

	@Override
	public void update(ProjeckpiVo bean) throws Exception {
		Projeckpi pojo = (Projeckpi) hibernateHandler.find(Projeckpi.class,
				bean.getProject_kpi_id());
		BeanUtils.copyProperties(bean, pojo);
		hibernateHandler.update(pojo);
	}

	@Override
	public void updateFlag(ProjeckpiVo bean) throws Exception {
		update(bean);
	}

	@Override
	public ProjeckpiVo find(Integer id) throws Exception {
		Projeckpi pojo = (Projeckpi) hibernateHandler.find(Projeckpi.class, id);
		ProjeckpiVo bean = new ProjeckpiVo();
		BeanUtils.copyProperties(pojo, bean);
		return bean;
	}

	@SuppressWarnings("unchecked")
	private Projeckpi findByProjectAndKpi(Integer project_id, String kpi_num)
			throws Exception {
		StringBuilder hql = new StringBuilder(
				"from Projeckpi where project.project_id=:project_id and kpi_num=:kpi_num");
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("project_id", project_id);
		params.put("kpi_num", kpi_num);
		List<Projeckpi> list = hibernateHandler.query(hql, params);
		if (list != null && list.size() > 0) {
			return list.get(0);
		} else {
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ProjeckpiVo> searchAllKpi(Integer project_id) throws Exception {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("project_id", project_id);
		StringBuilder hql = new StringBuilder("from Projeckpi where disable=0 and project.project_id=:project_id");
		List<Projeckpi> list = hibernateHandler.query(hql, params);
		List<ProjeckpiVo> d = new ArrayList<ProjeckpiVo>();
		for (Projeckpi bean : list) {
			ProjeckpiVo dv = new ProjeckpiVo();
			BeanUtils.copyProperties(bean, dv);
			d.add(dv);
		}
		return d;
	}
}
